//
//  ZCAFNetWork.m
//  CarWorld
//
//  Created by dllo on 15/9/1.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "ZCAFNetWork.h"
#import "AFNetworking.h"
@implementation ZCAFNetWork

+(void)getUrl:(NSString *)url
         body:(id)body
       result:(ZCResult)result
   headerFile:(NSDictionary *)headerFile
      success:(void (^)(id result))success
      failure:(void(^)(NSError *error))failure
{
    
    
    //1.获取网络请求管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    
    //3.给网络请求加请求头
    if (headerFile) {
        for (NSString *key in headerFile.allKeys) {
            [manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];
        }
    }
    
    //4.设置返回值类型
    switch (result) {
        case ZCData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case ZCJSON:
            break;
        case ZCXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }

    //2.设置网络请求返回值支持的参数类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    //5.发送网络请求
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            //成功回调
            success(responseObject);
            NSString *str = [url stringByReplacingOccurrencesOfString:@"/" withString:@""];
    
            // 缓存
            NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            
            NSString *sandBoxPath = sandBox[0];
            NSString *caches = [sandBoxPath stringByAppendingPathComponent:str];
            
            [NSKeyedArchiver archiveRootObject:responseObject toFile:caches];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error) {
            //失败回调
            failure(error);
            
            NSString *str = [url stringByReplacingOccurrencesOfString:@"/" withString:@""];
            NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            
            NSString *sandBoxPath = sandBox[0];
            NSString *caches = [sandBoxPath stringByAppendingPathComponent:str];
            
            id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:caches];
            if (responseObject != nil) {
                success(responseObject);
            }
        }
    }];
}

//post请求
+ (void)postUrl:(NSString *)url
           body:(id)body
         result:(ZCResult)result
   requestStyle:(ZCRequestStyle)requestStyle
     headerFile:(NSDictionary *)headerFile
        success:(void (^)(id result))success
        failure:(void (^)(NSError *error))failure
{
    // 1. 获取网络请求管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // 3. 网络请求请求体的body体类型
    switch (requestStyle) {
        case ZCRequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case ZCRequestString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError **error) {
                return parameters;
            }];
            break;
        default:
            break;
    }
    
    // 4. 给网络请求加请求头
    if (headerFile) {
        for (NSString *key in headerFile.allKeys) {
            [manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];
        }
    }
    // 5. 设置返回值类型
    switch (result) {
        case ZCData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case ZCJSON:
            break;
        case ZCXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    // 2. 设置网络请求返回值所支持的参数类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    // 6. 发送网络请求
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 成功回调
        if (responseObject) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 失败回调
        if (error) {
            failure(error);
        }
        
    }];
}

@end
